package com.ltsw.mvpbaseproject.basePresenter;

import android.content.Context;

import com.ltsw.mvpbaseproject.baseView.BaseActivity;
import com.ltsw.mvpbaseproject.baseView.BaseView;
import com.lzy.okgo.OkGo;

import java.lang.ref.WeakReference;

/**
 * @author FangHeQiang
 * @description:
 * @date :2019/8/28 13:55
 */
public class BasePresenter<V extends BaseView> {

    public V mView;
    private WeakReference<V> mReference;
    public BaseActivity mContext;

    public Context getContext() {
        return mContext;
    }

    /**
     * 弱引用
     */
    public void attachView(V view, BaseActivity context) {
        mReference = new WeakReference<V>(view);
        mView = mReference.get();
        mContext = context;
    }

    /**
     * 解除绑定
     */
    public void detachView() {
        if (mReference != null) {
            mReference.clear();
        }
    }

    public void showLoading() {
        if (mContext != null) {
            mContext.showLoading();
        }
    }

    public void hideLoading() {
        if (mContext != null) {
            mContext.hideLoading();
        }
    }

    /**
     * 取消网络请求
     */
    public void cancelRequest() {
        OkGo.getInstance().cancelTag(this);
    }


}
